package com.pussinboots.morning.cms.modules.goods.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.pussinboots.morning.cms.modules.system.category.dto.OsCategoryDto;
import com.pussinboots.morning.cms.modules.system.category.service.IOsCategoryService;
import com.pussinboots.morning.cms.modules.system.product.service.IOsProductService;
import com.pussinboots.morning.common.controller.BaseController;
import com.pussinboots.morning.common.model.PageInfo;

/**
 * 
* 项目名称：morning-cms-webapp   
* 类名称：SystemVersionLogControlller   
* 类描述：版本日至表示层控制器    
* 创建人：陈星星   
* 创建时间：2017年2月17日 上午12:32:07
 */
@Controller
@RequestMapping(value = "/system/goods")
public class GoodsListControlller extends BaseController {
	
	/** 管理员列表 */
	private static final String SYSTEM_GOODS_LIST = getViewPath("modules/goods/system_goods_list");
	
	@Autowired
	private IOsProductService productService;
	@Autowired
	private IOsCategoryService categoryService;
	

	
	/**
	 * 产品管理首页
	 * @param model
	 * @return
	 */
	@GetMapping(value = { "", "/list" })
	public String list(Model model,Integer offset,Integer limit , String order) {
		
		// 商品总数量
		Integer allGoodsNumber = productService.selectAllProductsNumber(null);
		model.addAttribute("allGoodsNumber", allGoodsNumber);
		
		//上架商品
		Integer onGoodsNumber = productService.selectAllProductsNumber(1);
		model.addAttribute("onGoodsNumber", onGoodsNumber);	
		
		//下架商品
		Integer outGoodsNumber = productService.selectAllProductsNumber(0);
		model.addAttribute("outGoodsNumber", outGoodsNumber);
		
		// 类目列表
		List<OsCategoryDto> osCategoryDtoList = categoryService.selectCategoryAndNumber(1);
		model.addAttribute("osCategoryDtoList", osCategoryDtoList);
		
		return SYSTEM_GOODS_LIST;
	}
	
	/**
	 * 产品列表
	 * @param model 
	 * @param pageInfo 分页条件
	 * @return
	 */
	@ResponseBody
	//@RequiresPermissions("system:goods:list")
	@PostMapping(value = {"/list/data" },produces=MediaType.APPLICATION_JSON_UTF8_VALUE)
	public PageInfo lists(Model model, @RequestBody JSONObject jsonParam) {
		
		//获取查询下条件
		Integer limit = 0;
		Integer offset = 0;
		String sortStr = "createTime";
		String order ="desc";
		
		if(jsonParam.containsKey("offset")){
			offset = jsonParam.getInteger("offset");
		}
		
		if(jsonParam.containsKey("limit")){
			limit = jsonParam.getInteger("limit");
		}
		
		if(jsonParam.containsKey("sort")){
			sortStr = jsonParam.getString("sort")!=null && !jsonParam.getString("sort").equals("") ? jsonParam.getString("sort") : sortStr;
		}
		
		if(jsonParam.containsKey("order")){
			order = jsonParam.getString("order");
		}
		
		//设置查询条件
		PageInfo pageInfo = new PageInfo();
		pageInfo.initPageSet(offset, limit, sortStr, order);
		
		//查询数据
		pageInfo = productService.selectByPullPage(pageInfo);
		
		
		return pageInfo;
	}
	
	
	
}
